package com.lxs.votesystem.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.lxs.votesystem.bean.VoteResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.lxs.votesystem.bean.Vote;
import com.lxs.votesystem.bean.VoteOption;
import com.lxs.votesystem.mapper.VoteMapper;
import com.lxs.votesystem.mapper.VoteOptionMapper;
import com.lxs.votesystem.service.VoteService;
import com.lxs.votesystem.util.AddVoteFormBean;

@Service
@Transactional
public class VoteServiceImpl implements VoteService {

	@Autowired
	private VoteMapper voteMapper;
	@Autowired
	private VoteOptionMapper voteOptionMapper;

	/*public void addVote(Vote vote, List<VoteOption> voteOptions) {
		// 调用mapper,执行操作
		int count1 = voteMapper.save(vote);
		for (VoteOption voteOption : voteOptions) {
			voteOption.setVoteID(vote.getVoteID());
		}
		int count2 = voteOptionMapper.saveBatch(voteOptions);
		if (count1 != 1 || count2 != voteOptions.size()) {
			throw new RuntimeException("数据库操作失败");
		}
	}*/

	
	  public void addVote(AddVoteFormBean addVoteFormBean) { 
		  //封装vote对象
		  Vote vote = new Vote(); 
		  vote.setVoteName(addVoteFormBean.getVoteName());
		  vote.setChannelID(addVoteFormBean.getChannelID());
		  // 调用voteMapper,保存
		  int count1 = voteMapper.save(vote); 
		  
		  // 封装VoteOption
		  List<String> voteOptionNames = addVoteFormBean.getVoteOptionNames(); 
		  List<VoteOption> voteOptions = new ArrayList<VoteOption>();
		  for(String voteOptionName : voteOptionNames) {
			  VoteOption voteOption = new VoteOption();
			  voteOption.setVoteOptionName(voteOptionName);
			  voteOption.setVoteID(vote.getVoteID());
			  voteOption.setTicketNum(0);
			  voteOptions.add(voteOption);
		  } 
		  // 调用voteOptionMapper批量保存
		  int count2 = voteOptionMapper.saveBatch(voteOptions); 
		  // 判断是否插入成功
		  if (count1 != 1 || count2 != voteOptions.size()) { 
			  throw new RuntimeException("数据库操作失败"); } 
		  }

	public List<VoteResult> queryAllVote() {
	  	// 定义所有投票的集合
		List<VoteResult> voteResults = new ArrayList<VoteResult>();
	  	// 调用mapper层
		// 查询所有投票
		List<Vote> votes = voteMapper.queryAll();
		// 查询所有投票选型
		for (Vote vote : votes) {
			List<VoteOption> voteOptions = voteOptionMapper.queryAllByVoteID(vote.getVoteID());
			// 定义VoteResult对象
			VoteResult voteResult = new VoteResult();
			// 封装VoteResult
			voteResult.setVote(vote);
			voteResult.setVoteOptions(voteOptions);
			// 将封装好的数据放到集合中
			voteResults.add(voteResult);
		}
		return voteResults;
	}

	/**
	 * 删除投票
	 *
	 * @param voteID
	 */
	@Override
	public void delVote(Integer voteID) {
		// 根据voteID,查询投票选项的数量
		Integer countOption = voteOptionMapper.queryCountByVoteID(voteID);
		// 调用voteOptionMapper,删除投票选项
		Integer count1 = voteOptionMapper.removeVoteOptionByVoteID(voteID);
		// 调用voteMapper,删除投票
		Integer count2 = voteMapper.removeVoteByVoteID(voteID);
		if (count1 != countOption || count2 != 1) {
			throw new RuntimeException("数据库操作失败");
		}
	}

	/**
	 * 根据频道id,查询该频道下的所有投票信息
	 *
	 * @param channelID
	 * @return
	 */
	@Override
	public List<VoteResult> queryVoteResultsByCID(Integer channelID) {
		// 定义所有投票的集合
		List<VoteResult> voteResults = new ArrayList<VoteResult>();
		// 调用mapper层
		// 查询所有投票
		List<Vote> votes = voteMapper.queryVoteResultsByCID(channelID);
		// 查询所有投票选型
		for (Vote vote : votes) {
			List<VoteOption> voteOptions = voteOptionMapper.queryAllByVoteID(vote.getVoteID());
			// 定义VoteResult对象
			VoteResult voteResult = new VoteResult();
			// 封装VoteResult
			voteResult.setVote(vote);
			voteResult.setVoteOptions(voteOptions);
			// 将封装好的数据放到集合中
			voteResults.add(voteResult);
		}
		return voteResults;
	}

	/**
	 * 投票
	 *
	 * @param voteOptionID
	 */
	@Override
	public void doVote(Integer voteOptionID) {
		int count = voteOptionMapper.updateTicketNum(voteOptionID);
		if (count != 1) {
			throw new RuntimeException("数据库操作失败");
		}
	}

	/**
	 * 根据voteID,获得voteresult
	 *
	 * @param voteID
	 * @return
	 */
	@Override
	public VoteResult getVoteResultByVoteID(Integer voteID) {
		// 调用voteMapper查询vote
		Vote vote = voteMapper.getVoteByID(voteID);
		// 调用voteOptionMapper 根据voteID查询voteOption
		List<VoteOption> voteOptions = voteOptionMapper.queryAllByVoteID(voteID);

		// 封装voteresult
		VoteResult voteResult = new VoteResult();
		voteResult.setVote(vote);
		voteResult.setVoteOptions(voteOptions);
		return voteResult;
	}
}
